<template>
  <Panel>
    <template #title>订单管理</template>
    <template>
      <!-- 1.0 查询的表单 -->
      <el-form
        size="mini"
        :inline="true"
        :model="searchObj"
        class="demo-form-inline"
      >
        <el-form-item label="订单号">
          <el-input v-model="searchObj.orderNo" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input
            v-model="searchObj.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchObj.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchObj.orderState" placeholder="订单状态">
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="选择时间">
          <el-date-picker
            v-model="searchObj.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getAllOrders('s')">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 2.0 渲染数据的表格 -->
      <el-table :data="tableData" border style="width: 100%" size="mini">
        <el-table-column fixed prop="orderNo" label="订单号"> </el-table-column>
        <el-table-column label="下单时间" width="150">
          <template v-slot="{ row }">
            {{ row.orderTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="120">
        </el-table-column>
        <el-table-column label="送达时间" width="120">
          <template v-slot="{ row }">
            {{ row.deliveryTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注"> </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额"> </el-table-column>
        <el-table-column prop="orderState" label="订单状态"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button @click="handleClick(row)" type="text" size="small"
              >查看</el-button
            >
            <!-- @click="$router.push('/orders/edit?id=' + row.id)" -->
            <!-- $router.push({ path: '/orders/edit', query: { id: row.id } }) -->
            <!-- @click="$router.push(`/orders/edit/${row.id}`)"  必须配合路由配置 -->
            <!-- $router.push({ name: 'orderEdit', params: { id: row.id } })  -->
            <!-- 最后一种如果不保留参数，则刷新就没有参数 -->
            <el-button
              type="text"
              size="small"
              @click="
                $router.push({ name: 'orderEdit', params: { id: row.id } })
              "
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.0 分页处理 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 4.0 查看订单详情 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <div>
          <p>{{ curData }}</p>
          <el-timeline :reverse="true">
            <el-timeline-item
              v-for="(val, index) in orderStates"
              :key="index"
              :timestamp="val.time"
            >
              {{ val.name }}
            </el-timeline-item>
          </el-timeline>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { allOrders, searchOrders } from '@/api/orders'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      // 查询参数
      searchObj: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      },
      dialogVisible: false,
      curData: {},
      orderStates: [
        {
          name: '已受理',
          time: '2020-05-10 12:00'
        },
        {
          name: '派送中',
          time: '2020-05-10 13:00'
        },
        {
          name: '已完成',
          time: '2020-05-10 17:00'
        }
      ]
    }
  },
  created() {
    this.getAllOrders()
  },
  methods: {
    async getAllOrders(s) {
      // 时间的date需要是字符串的数组
      const searchData = JSON.parse(JSON.stringify(this.searchObj))
      searchData.date = JSON.stringify(searchData.date)
      const obj = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...searchData
      }
      let res = null
      if (s === 's') {
        // 查询  searchOrders
        obj.currentPage = 1
        res = await searchOrders(obj)
      } else {
        // 正常请求
        res = await allOrders(obj)
      }
      this.tableData = res.data
      this.total = res.total
    },
    handleClick(row) {
      this.dialogVisible = true
      console.log(row)
      this.curData = row
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getAllOrders()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getAllOrders()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 40px;
}
</style>
